﻿using UnityEngine;
using System.Collections;
using System;

public class textureCommand : ICommand {

	private int width;
	private int height;
	private bool askfordata = false;
	private Texture2D texture;
	private bool needResponse = false;

	public textureCommand() {
		askfordata = false;
		needResponse = false;
	}

	public string getCommand() {
		return "";
	}

	public void respond(string command, string content) {
		if (command.Equals ("sometime")) {
			string[] parts = content.Split(',');
			if(parts.Length == 2) {
				width = Int32.Parse(parts[0]);
				height = Int32.Parse(parts[1]);
				texture = new Texture2D(width, height);
				// now ask for data
				needResponse = true;
				askfordata = true;
			}
		// Do something with the texture data
		} else if (command.Equals ("texture")) {
			Debug.Log("Received Texture");
			Debug.Log(content);
			byte[] data = System.Text.Encoding.ASCII.GetBytes (content);
			texture = new Texture2D(256,256);
			bool loaded = texture.LoadImage(data);
			if(loaded == false) Debug.Log ("Loading Failed");
			else Debug.Log("Loaded okay");
			GameManager.getInstance().mapTexture = texture;
			if(Application.loadedLevel == 3) {
				Debug.Log ("Setting Texture now");
				GameObject.Find("Map").GetComponent<Renderer>().material.mainTexture = texture;
				Debug.Log("Texture Set");
			}
			// now do something with the texture
		}
	}

	public bool needsToRespond() {
		return false;
	}
}
